<template>
  <TablePanel title="商机管理">
    <div slot="btm">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        @click="handleAdd"
        v-hasPermi="['crm:business:add']"
      >新增</el-button>
      <el-button
        type="success"
        plain
        icon="el-icon-edit"
        :disabled="single"
        @click="handleUpdate"
        v-hasPermi="['crm:business:edit']"
      >修改</el-button>
      <el-button
        type="danger"
        plain
        icon="el-icon-delete"
        :disabled="multiple"
        @click="handleDelete"
        v-hasPermi="['crm:business:remove']"
      >删除</el-button>
      <el-button
        type="warning"
        plain
        icon="el-icon-download"
        @click="handleExport"
        v-hasPermi="['crm:business:export']"
      >导出</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>
    <div slot="search">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="商机名称" prop="businessName">
          <el-input
            v-model="queryParams.businessName"
            placeholder="请输入商机名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="商机阶段：" prop="statusId">
          <el-input
            v-model="queryParams.statusId"
            placeholder="请输入商机阶段："
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div slot="content">
      <el-table v-loading="loading" :height="tableHeight" border :data="businessList" @selection-change="handleSelectionChange">
        <div slot="empty">
          <el-empty :image="noData" image-size="200">
          </el-empty>
        </div>
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="商机编号" align="center" prop="businessId" />
        <el-table-column label="商机名称" align="center" prop="businessName" />
        <el-table-column label="商机金额" align="center" prop="money" />
        <el-table-column label="商机阶段" align="center" prop="statusId" />
        <el-table-column label="下次联系时间" align="center" prop="nextTime" />
        <el-table-column label="客户" align="center" prop="customerId" />
        <el-table-column label="预计成交日期" align="center" prop="dealDate" />
        <el-table-column label="整单折扣" align="center" prop="discountRate" />
        <el-table-column label="产品总金额" align="center" prop="totalPrice" />
        <el-table-column label="商机来源" align="center" prop="source" />
        <el-table-column label="创建人" align="center" prop="createdBy" />
        <el-table-column label="负责人" align="center" prop="ownerUserId" />
        <el-table-column label="创建时间" align="center" prop="createdTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['crm:business:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['crm:business:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>



      <!-- 添加或修改商机对话框 -->
      <st-dialog :title="title" v-if="open" @close="cancel"
                 @save="submitForm"  append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="商机名称：" prop="businessName">
            <el-input v-model="form.businessName" placeholder="请输入商机名称" />
          </el-form-item>
          <el-form-item label="商机金额：" prop="money">
            <el-input v-model="form.money" placeholder="请输入商机金额" />
          </el-form-item>
          <el-form-item label="商机阶段：" prop="statusId">
            <el-input v-model="form.statusId" placeholder="请输入商机阶段" />
          </el-form-item>
          <el-form-item label="下次联系时间：" prop="nextTime">
            <el-input v-model="form.nextTime" placeholder="请输入下次联系时间" />
          </el-form-item>
          <el-form-item label="客户：" prop="customerId">
            <el-input v-model="form.customerId" placeholder="请输入客户" />
          </el-form-item>
          <el-form-item label="预计成交日期：" prop="dealDate">
            <el-input v-model="form.dealDate" placeholder="请输入预计成交日期" />
          </el-form-item>
          <el-form-item label="整单折扣：" prop="discountRate">
            <el-input v-model="form.discountRate" placeholder="请输入整单折扣" />
          </el-form-item>
          <el-form-item label="产品总金额：" prop="totalPrice">
            <el-input v-model="form.totalPrice" placeholder="请输入产品总金额" />
          </el-form-item>
          <el-form-item label="商机来源：" prop="source">
            <el-input v-model="form.source" placeholder="请输入商机来源" />
          </el-form-item>
          <el-form-item label="备注：" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="负责人：" prop="ownerUserId">
            <el-input v-model="form.ownerUserId" placeholder="请输入负责人" />
          </el-form-item>
        </el-form>
      </st-dialog>
    </div>
    <div slot="footer">
      <pagination
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </TablePanel>
</template>

<script>
import { listBusiness, getBusiness, delBusiness, addBusiness, updateBusiness } from "@/api/crm/business";
import noData from '@/assets/img/no_data.png'
export default {
  name: "Business",
  data() {
    return {
      noData:noData,
      tableHeight:undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商机表格数据
      businessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessName: null,
        money: null,
        statusId: null,
        nextTime: null,
        customerId: null,
        dealDate: null,
        discountRate: null,
        totalPrice: null,
        source: null,
        roUserId: null,
        rwUserId: null,
        isEnd: null,
        createdBy: null,
        ownerUserId: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商机列表 */
    getList() {
      this.loading = true;
      listBusiness(this.queryParams).then(response => {
        this.businessList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.$nextTick(()=>{
          this.tableHeight=this.getTableHeightWithTitle();
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        businessId: null,
        businessName: null,
        money: null,
        statusId: null,
        nextTime: null,
        customerId: null,
        dealDate: null,
        discountRate: null,
        totalPrice: null,
        source: null,
        remark: null,
        roUserId: null,
        rwUserId: null,
        isEnd: null,
        createdBy: null,
        ownerUserId: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.businessId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商机";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const businessId = row.businessId || this.ids
      getBusiness(businessId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商机";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.businessId != null) {
            updateBusiness(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBusiness(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const businessIds = row.businessId || this.ids;
      this.$modal.confirm('是否确认删除商机编号为"' + businessIds + '"的数据项？').then(function() {
        return delBusiness(businessIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crm/business/export', {
        ...this.queryParams
      }, `business_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
